<html>
<head><meta charset="utf-8"><title>Higher ranked trait inheritance · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Higher.20ranked.20trait.20inheritance.html">Higher ranked trait inheritance</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="245337362"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Higher%20ranked%20trait%20inheritance/near/245337362" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Higher.20ranked.20trait.20inheritance.html#245337362">(Jul 08 2021 at 16:26)</a>:</h4>
<p>Hi, I want to make a trait that "ineritance" from another trait but require that other trait to be implemented both on Self and on &amp;Self</p>
<p>More concretely,<br>
I want to be able to uncomment <code>add3</code>:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">core</span>::<span class="n">ops</span>::<span class="n">Add</span><span class="p">;</span><span class="w"></span>
<span class="k">trait</span><span class="w"> </span><span class="n">BigInt</span>: <span class="nc">Add</span><span class="o">&lt;</span><span class="n">Output</span><span class="o">=</span><span class="bp">Self</span><span class="o">&gt;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">Sized</span><span class="w"></span>
<span class="k">where</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="w"> </span><span class="bp">Self</span>: <span class="nc">Add</span><span class="o">&lt;&amp;'</span><span class="na">a</span><span class="w"> </span><span class="bp">Self</span><span class="p">,</span><span class="w"> </span><span class="n">Output</span><span class="o">=</span><span class="bp">Self</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="c1">//for&lt;'a&gt; &amp;'a Self: Add&lt;Self, Output=Self&gt;</span>
<span class="p">{</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">add</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">BigInt</span><span class="o">&gt;</span><span class="p">(</span><span class="n">a</span>: <span class="nc">T</span><span class="p">,</span><span class="w"> </span><span class="n">b</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">a</span><span class="o">+</span><span class="n">b</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">add2</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">BigInt</span><span class="o">&gt;</span><span class="p">(</span><span class="n">a</span>: <span class="nc">T</span><span class="p">,</span><span class="w"> </span><span class="n">b</span>: <span class="kp">&amp;</span><span class="nc">T</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">a</span><span class="o">+</span><span class="n">b</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="c1">// fn add3&lt;T: BigInt&gt;(a: &amp;T, b: T) -&gt; T {</span>
<span class="c1">//     a+b</span>
<span class="c1">// }</span>
</code></pre></div>
<p><a href="https://play.rust-lang.org/?gist=bf5743791966d8cb8f9c97690a8c6953">https://play.rust-lang.org/?gist=bf5743791966d8cb8f9c97690a8c6953</a></p>
<p>Is this possible? or do I have to make a wrapper type and implement the ops traits manually on that type?</p>



<a name="245337620"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Higher%20ranked%20trait%20inheritance/near/245337620" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Higher.20ranked.20trait.20inheritance.html#245337620">(Jul 08 2021 at 16:29)</a>:</h4>
<p>I think you can only do this by duplicating the where-clause to <code>add3</code> (<code>where for&lt;'a&gt; &amp;'a T: Add&lt;T, Output=T&gt;</code>)</p>



<a name="245337670"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Higher%20ranked%20trait%20inheritance/near/245337670" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Higher.20ranked.20trait.20inheritance.html#245337670">(Jul 08 2021 at 16:29)</a>:</h4>
<p>Only trait bounds of the form <code>Self: $supertrait</code> are propagated to users of the trait</p>



<a name="245337755"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Higher%20ranked%20trait%20inheritance/near/245337755" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Higher.20ranked.20trait.20inheritance.html#245337755">(Jul 08 2021 at 16:30)</a>:</h4>
<p><span class="user-mention" data-user-id="211727">@Jonas Schievink  [he/him]</span> the first <code>for</code> bound did work though, if you uncomment it <code>add2</code> will no longer compile.</p>



<a name="245337776"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Higher%20ranked%20trait%20inheritance/near/245337776" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Higher.20ranked.20trait.20inheritance.html#245337776">(Jul 08 2021 at 16:30)</a>:</h4>
<p>Yeah, the propagation works even behind <code>for&lt;'a&gt;</code> quantifiers</p>



<a name="245337823"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Higher%20ranked%20trait%20inheritance/near/245337823" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Higher.20ranked.20trait.20inheritance.html#245337823">(Jul 08 2021 at 16:31)</a>:</h4>
<p>This is <a href="https://github.com/rust-lang/rust/issues/20671">https://github.com/rust-lang/rust/issues/20671</a>, I believe</p>



<a name="245337999"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Higher%20ranked%20trait%20inheritance/near/245337999" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Higher.20ranked.20trait.20inheritance.html#245337999">(Jul 08 2021 at 16:32)</a>:</h4>
<p>This also looks like <a href="https://github.com/rust-lang/rust/issues/86635">#86635</a></p>



<a name="245338030"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Higher%20ranked%20trait%20inheritance/near/245338030" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Higher.20ranked.20trait.20inheritance.html#245338030">(Jul 08 2021 at 16:32)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink  [he/him]</span> <a href="#narrow/stream/122651-general/topic/Higher.20ranked.20trait.20inheritance/near/245337823">said</a>:</p>
<blockquote>
<p>This is <a href="https://github.com/rust-lang/rust/issues/20671">https://github.com/rust-lang/rust/issues/20671</a>, I believe</p>
</blockquote>
<p>So if I want a single "thing" to encapsulate this ability I must use a new <em>type</em>(wrapping a generic) and not a trait?</p>



<a name="245541157"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Higher%20ranked%20trait%20inheritance/near/245541157" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Higher.20ranked.20trait.20inheritance.html#245541157">(Jul 10 2021 at 09:54)</a>:</h4>
<p>Even trying to use a new type doesn't help much, because it means I need to add the <code>where</code> clauses to the all the functions using that type: <a href="https://play.rust-lang.org/?gist=4220b736529bb91f15034fc0ec4b01f5">https://play.rust-lang.org/?gist=4220b736529bb91f15034fc0ec4b01f5</a></p>



<a name="245541856"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Higher%20ranked%20trait%20inheritance/near/245541856" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Higher.20ranked.20trait.20inheritance.html#245541856">(Jul 10 2021 at 10:12)</a>:</h4>
<p><span class="user-mention" data-user-id="138448">@cuviper</span> I see you asked this in the forum before, did you find any solution to this? <br>
(<a href="https://users.rust-lang.org/t/traits-with-self-bounds/4033">https://users.rust-lang.org/t/traits-with-self-bounds/4033</a>)</p>



<a name="245553352"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Higher%20ranked%20trait%20inheritance/near/245553352" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Higher.20ranked.20trait.20inheritance.html#245553352">(Jul 10 2021 at 14:31)</a>:</h4>
<p>AFAIK the situation is still the same with <a href="https://github.com/rust-lang/rust/issues/20671">#20671</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>